package com.example.s.Activity;

import androidx.annotation.Nullable;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.BasePickerView;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.s.R;
import com.example.s.utils.MyUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class UserInfoActivity extends BaseActivity {
    private ImageView back;
    private CircleImageView headPhoto;
    private ImageView editPhoto;
    private EditText sign;
    private EditText nickname;
    private TextView sexTitle;
    private TextView sex;
    private TextView birthTitle;
    private TextView birth;
    private EditText job;
    private TextView areaTitle;
    private TextView area;
    private EditText school;
    private EditText introduce;
    private TextView save;
    //  省份
    ArrayList<String> provinceBeanList = new ArrayList<>();
    //  城市
    ArrayList<String> cities;
    ArrayList<List<String>> cityList = new ArrayList<>();
    //  区/县
    ArrayList<String> district;
    ArrayList<List<String>> districts;
    ArrayList<List<List<String>>> districtList = new ArrayList<>();

    private void initView() {


        back = (ImageView) findViewById(R.id.back);
        headPhoto = (CircleImageView) findViewById(R.id.headPhoto);
        editPhoto = (ImageView) findViewById(R.id.editPhoto);
        sign = (EditText) findViewById(R.id.sign);
        nickname = (EditText) findViewById(R.id.nickname);
        sexTitle = (TextView) findViewById(R.id.sexTitle);
        sex = (TextView) findViewById(R.id.sex);
        birthTitle = (TextView) findViewById(R.id.birthTitle);
        birth = (TextView) findViewById(R.id.birth);
        job = (EditText) findViewById(R.id.job);
        areaTitle = (TextView) findViewById(R.id.areaTitle);
        area = (TextView) findViewById(R.id.area);
        school = (EditText) findViewById(R.id.school);
        introduce = (EditText) findViewById(R.id.introduce);
        save = (TextView) findViewById(R.id.save);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        initView();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        editPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("android.intent.action.GET_CONTENT");
                intent.setType("image/*");
                startActivityForResult(intent, 2);
            }
        });

        List<String> sexOptions = new ArrayList<>();
        sexOptions.add("男");
        sexOptions.add("女");
        sex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OptionsPickerBuilder optionsPickerBuilder = new OptionsPickerBuilder(UserInfoActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        Log.d("", "onOptionsSelect: " + sexOptions.get(options1));
                        sex.setText(sexOptions.get(options1));
                    }
                });
                OptionsPickerView pvCustomOptions = (OptionsPickerView) setOptionPickerStyle(optionsPickerBuilder);
                pvCustomOptions.setPicker(sexOptions);
                pvCustomOptions.show();
            }
        });

        birth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimePickerView pvTime = new TimePickerBuilder(UserInfoActivity.this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        birth.setText(MyUtils.getInstance().getDateString(date));
                    }

                }).setBgColor(Color.parseColor("#000000"))
                        .setTitleBgColor(Color.parseColor("#0a0a0a"))
                        .setTextColorCenter(Color.parseColor("#ffffff"))
                        .setTextColorOut(Color.parseColor("#575757"))
                        .setDividerColor(Color.parseColor("#1c1c27"))
                        .setSubmitColor(0xFFF5F6FA)
                        .setCancelColor(0xFFF5F6FA)
                        .setCancelText("取消")//取消按钮文字
                        .setSubmitText("确定")//确认按钮文字
                        .isCenterLabel(false)
                        .setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                        .build();
                pvTime.show();
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                parseJson(MyUtils.getInstance().getFromRaw(UserInfoActivity.this, R.raw.area));
            }
        }).start();

        area.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OptionsPickerBuilder optionsPickerBuilder = new OptionsPickerBuilder(UserInfoActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        area.setText(provinceBeanList.get(options1) + "-" + cityList.get(options1).get(options2) + "-" + districtList.get(options1).get(options2).get(options3));
                    }
                });
                OptionsPickerView pvCustomOptions = (OptionsPickerView) setOptionPickerStyle(optionsPickerBuilder);
                pvCustomOptions.setPicker(provinceBeanList, cityList, districtList);
                pvCustomOptions.show();

            }
        });

    }


    /**
     * 解析json填充集合
     *
     * @param str 待解析的json，获取省市县
     */
    private void parseJson(String str) {
        try {
            //  获取json中的数组
            JSONArray jsonArray = new JSONArray(str);
            //  遍历数据组
            for (int i = 0; i < jsonArray.length(); i++) {
                //  获取省份的对象
                JSONObject provinceObject = jsonArray.optJSONObject(i);
                //  获取省份名称放入集合
                String provinceName = provinceObject.getString("name");
                provinceBeanList.add(provinceName);
                //  获取城市数组
                JSONArray cityArray = provinceObject.optJSONArray("children");
                //   声明存放城市的集合
                cities = new ArrayList<>();
                //声明存放区县集合的集合
                districts = new ArrayList<>();
                //  遍历城市数组
                for (int j = 0; j < cityArray.length(); j++) {
                    //  获取城市对象
                    JSONObject cityObject = cityArray.optJSONObject(j);
                    //  将城市放入集合
                    String cityName = cityObject.optString("name");
                    cities.add(cityName);

                    // 声明存放区县的集合
                     district= new ArrayList<>();
                    //  获取区县的数组
                    JSONArray areaArray = cityObject.optJSONArray("children");
                    //  遍历区县数组，获取到区县名称并放入集合
                    if (areaArray != null) {
                        for (int k = 0; k < areaArray.length(); k++) {
                            String areaName = areaArray.optJSONObject(k).optString("name");
                            district.add(areaName);
                        }
                        //  将区县的集合放入集合
                        districts.add(district);
                    }
                }
                //  将存放区县集合的集合放入集合
                districtList.add(districts);
                //  将存放城市的集合放入集合
                cityList.add(cities);
            }
            Log.d(TAG, "parseJson: 解析成功");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private BasePickerView setOptionPickerStyle(OptionsPickerBuilder pickerView){

        OptionsPickerView<Object> pickerView1 = pickerView.setBgColor(Color.parseColor("#000000"))
                .setTitleBgColor(Color.parseColor("#0a0a0a"))
                .setTextColorCenter(Color.parseColor("#ffffff"))
                .setTextColorOut(Color.parseColor("#575757"))
                .setDividerColor(Color.parseColor("#1c1c27"))
                .setSubmitColor(0xFFF5F6FA)
                .setCancelColor(0xFFF5F6FA)
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setSelectOptions(0)
                .isCenterLabel(false)
                .build();
        return pickerView1;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && data != null && resultCode == RESULT_OK) {
            Uri data1 = data.getData();
            headPhoto.setImageURI(data1);
            //保存图片请求

        }
    }
}
